package com.cw.controller;

import com.cw.beans.Asset;
import com.cw.beans.Maintain;
import com.cw.mapper.*;
import com.cw.util.DateUtils;
import com.cw.util.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class MaintainController {
    @Autowired
    private MaintainMapper maintainMapper;
    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private DepreciateMapper depreciateMapper;
    @Autowired
    private StateMapper stateMapper;

    @RequestMapping("/addMaintenance")
    public String addMaintenance(Maintain maintain, Model model) {
        System.out.println(maintain);
        int state = assetMapper.getState(maintain.getAsset_id());
        System.out.println(state);
        if (state == 3) {
            model.addAttribute("msg", "该设备着正在维修，不能再次维修");
            return "Maintenance_Apply";
        } else if (state == 4) {
            model.addAttribute("msg", "该设备已经报废，不能维修");
            return "Maintenance_Apply";
        } else {
            maintainMapper.addMaintain(maintain);
            assetMapper.updateAssetMaintain(maintain.getAsset_id());
            return "Maintenance_Apply";
        }

    }

    @ResponseBody
    @RequestMapping("/maintain")
    public Object maintain(String pagesize, String name, String type, String dept) {
        Map map = new HashMap();
        Maintain maintain = new Maintain();
        if (name != null && !name.equals("")) {
            maintain.setAsset_name(name);
        }
        Page page = new Page();
        page.setCurrentPage(pagesize == null ? 1 : Integer.parseInt(pagesize));
        //得到有多少
        ArrayList<Maintain> allAssets = maintainMapper.getMaintain(maintain, page, type, dept);

        if (allAssets.size() == 0) {
            map.put("mag", "没有数据");
            map.put("page", page);
            map.put("maintain", new Maintain());

            map.put("dept", deptMapper.getAllDept());

            map.put("type", typeMapper.gettype());

            map.put("state", stateMapper.getState());
            return map;
        } else {
            map.put("list", allAssets);
            int num = maintainMapper.getMaintainnNum(maintain, type, dept);
            page.setRecordTotal(num);
            map.put("page", page);
            map.put("type", typeMapper.gettype());
            map.put("dept", deptMapper.getAllDept());
            map.put("state", stateMapper.getState());
            map.put("mainthin", maintain);
            return map;
        }

    }

    //维修完毕
    @ResponseBody
    @RequestMapping("/maintainout")
    public Map eturnBook(String id,String asset_id, String pagesize, String name, String type, String dept) {
        Map map = new HashMap();
        if (id != null) {
            int id1 = Integer.parseInt(id);
            int  asset_id1 =   Integer.parseInt(asset_id);
            assetMapper.updateAssetMaintain(asset_id1);
            Maintain maintain = new Maintain();
            Date date = new Date();
            String s = DateUtils.formateDate(date);
            System.out.println(s);
            maintainMapper.updateMaintain(id1, s);
            if (name != null && !name.equals("")) {
                maintain.setAsset_name(name);
            }
            Page page = new Page();
            page.setCurrentPage(pagesize == null ? 1 : Integer.parseInt(pagesize));
            //得到有多少
            ArrayList<Maintain> allAssets = maintainMapper.getMaintain1(maintain, page, type, dept);

            if (allAssets.size() == 0) {
                map.put("mag", "没有数据");
                map.put("page", page);
                map.put("maintain", new Maintain());

                map.put("dept", deptMapper.getAllDept());

                map.put("type", typeMapper.gettype());

                map.put("state", stateMapper.getState());
                return map;
            } else {
                map.put("list", allAssets);
                int num = maintainMapper.getMaintainnNum1(maintain, type, dept);
                page.setRecordTotal(num);
                map.put("page", page);
                map.put("type", typeMapper.gettype());
                map.put("dept", deptMapper.getAllDept());
                map.put("state", stateMapper.getState());
                map.put("mainthin", maintain);
                return map;
            }
        } else {

            Maintain maintain = new Maintain();
            if (name != null && !name.equals("")) {
                maintain.setAsset_name(name);
            }
            Page page = new Page();
            page.setCurrentPage(pagesize == null ? 1 : Integer.parseInt(pagesize));
            //得到有多少
            ArrayList<Maintain> allAssets = maintainMapper.getMaintain1(maintain, page, type, dept);

            if (allAssets.size() == 0) {
                map.put("mag", "没有数据");
                map.put("page", page);
                map.put("maintain", new Maintain());

                map.put("dept", deptMapper.getAllDept());

                map.put("type", typeMapper.gettype());

                map.put("state", stateMapper.getState());
                return map;
            } else {
                map.put("list", allAssets);
                int num = maintainMapper.getMaintainnNum1(maintain, type, dept);
                page.setRecordTotal(num);
                map.put("page", page);
                map.put("type", typeMapper.gettype());
                map.put("dept", deptMapper.getAllDept());
                map.put("state", stateMapper.getState());
                map.put("mainthin", maintain);
                return map;
            }

        }
    }
}